package com.quanyan.club.task.tracker;

import com.alibaba.fastjson.JSON;
import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.club.constants.ClubConstants;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Author Hugo.Wwg
 * @Since 2016-07-04
 */
public class TaskJobRunner implements JobRunner {

    private static final Logger log = LoggerFactory.getLogger(TaskJobRunner.class);

    @Autowired
    private ActivityStartNotifyJob activityStartNotifyJob;
    @Autowired
    private ActivityStartAbleCheckJob activityStartAbleCheckJob;
    @Autowired
    private ClubIntegralStatsJob clubIntegralStatsJob;
    @Autowired
    private ActivityEndNotifyJob activityEndNotifyJob;
    @Autowired
    private ActivityStatusUpdateJob activityStatusUpdateJob;
    @Autowired
    private ActivityCreateWeekRangeJob activityCreateWeekRangeJob;
    @Autowired
    private ClubIMGroupidCheckJob clubIMGroupidCheckJob;
    @Autowired
    private ActivitySettlementJob activitySettlementJob;
    @Autowired
    private ClubFinanceSummaryJob clubFinanceSummaryJob;
    @Autowired
    private ClubTaskSummaryJob clubTaskNotifyJob;
    @Autowired
    private ClubIntegralTaskJob clubIntegralTaskJob;
    @Autowired
    private ActivityAutoAppraise activityAutoAppraise;
    @Autowired
    private ActivityCreateCycleRangeJob activityCreateCycleRangeJob;
    @Autowired
    private ActivitySettleRepeatJob activitySettleRepeatJob;
    @Autowired
    private ClubYearActivityJob clubYearActivityJob;
    @Autowired
    private PullClubSummaryJob pullClubSummaryJob;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        Job job = jobContext.getJob();
        log.warn("收到定时任务：" + JSON.toJSONString(job));
        String taskId = job.getTaskId();

        if (taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_START_NOTIFY)) {
            return activityStartNotifyJob.run(jobContext);

        } else if (taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_WEEK_RANGE_CREATE)) {
            return activityCreateWeekRangeJob.run(jobContext);

        } else if (taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_STATUS_UPDATE)) {
            return activityStatusUpdateJob.run(jobContext);

        }else if (taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_ABLE_STAR_CHECK)) {
            return activityStartAbleCheckJob.run(jobContext);

        } else if (taskId.contains(ClubConstants.CLUB_JOB_ID_CLUB_INTEGRAL_STATS)) {
            return clubIntegralStatsJob.run(jobContext); //调用俱乐部积分任务

        } else if (taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_END_NOTIFY)) {
            return activityEndNotifyJob.run(jobContext);

        } else if (taskId.contains(ClubConstants.CLUB_JOB_IM_GROUPIDMISSING_CHECK)){
            return clubIMGroupidCheckJob.run(jobContext);

        } else if(taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_SETTLE)){
            return activitySettlementJob.run(jobContext);

        }else if (taskId.contains(ClubConstants.CLUB_JOB_SUMMARY_FINANCE)){
            return clubFinanceSummaryJob.run(jobContext);   //财务统计任务

        }else if (taskId.contains(ClubConstants.CLUB_JOB_TASK_NOTIFY)){
            return clubTaskNotifyJob.run(jobContext);
        }else if(taskId.contains(ClubConstants.CLUB_JOB_ID_CLUB_INTEGRAL)){//第一次统计俱乐部积分用，后可去除 --create by xian-guo
            return clubIntegralTaskJob.run(jobContext);
        }else if(taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_AUTO_APPRAISE)){
            //自动评价
            return activityAutoAppraise.run(jobContext);
        }else if(taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_CYCLE_RANGE_CREATE)){
            //日循环
            return activityCreateCycleRangeJob.run(jobContext);
        }else if(taskId.contains(ClubConstants.CLUB_JOB_ID_ACTIVITY_AUTO_SETTLE)){
            //自动扫描结算
            return activitySettleRepeatJob.run(jobContext);
        }else if (taskId.contains(ClubConstants.CLUB_JOB_ID_YEAR_ACTIVITY_JOB)){//2017春节活动
            return clubYearActivityJob.run(jobContext);
        }else if (taskId.contains(ClubConstants.CLUB_JOB_ID_PULL_SUMMARY)){//渠道统计
            return pullClubSummaryJob.run(jobContext);
        }
        else {
            //没有匹配类型，默认提交成功
            return new Result(Action.EXECUTE_SUCCESS, "当前没有可执行的业务逻辑");
        }
    }
}
